package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController{
    @Autowired
    private HospitalSetService hospitalSetService;
    @ApiOperation("获取医院设置列表")
    @GetMapping("/findAll")
    public Result findAllHospital(){
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }
    @ApiOperation("逻辑删除医院设置")
    @ApiParam(name = "id",value = "医院ID")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable("id") Long id){
        boolean remove = hospitalSetService.removeById(id);
        if(remove){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //3、条件分页查询
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) HospitalQueryVo hospitalQueryVo){
        String hoscode = null;
        String hosname = null;
        if(null != hospitalQueryVo){
            hoscode = hospitalQueryVo.getHoscode();
            hosname = hospitalQueryVo.getHosname();
        }
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hoscode",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }
    //4、添加医院设置
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        String encrypt = MD5.encrypt(String.valueOf(System.
                currentTimeMillis())+random.nextInt(1000));
        hospitalSet.setSignKey(encrypt);
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //5、根据医院ID获取设置
    @ApiOperation("根据医院ID获取设置")
    @ApiParam(name = "id",value = "医院ID")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    //6、修改医院设置
    @ApiOperation("修改医院设置")
    @ApiParam(name = "hospitalSet",value = "医院设置对象")
    @PutMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //7、批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @ApiParam(name = "idList",value = "批量删除的医院设置ID")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean remove = hospitalSetService.removeByIds(idList);
        if(remove){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //8、医院锁定与解锁
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //9、发送签名密钥
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        //TODO 发送短信

        return Result.ok();
    }
}
